﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace VatGia
{
    public partial class FormDangKy : Form
    {
        FormMain1 frm;
        public FormDangKy(FormMain1 frm)
        {
            this.frm = frm;
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void txtCMND_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtHoTen_TextChanged(object sender, EventArgs e)
        {

        }
        DataService dservice = new DataService();
        DataTable dt = new DataTable();
        private void btnRegester_Click(object sender, EventArgs e)
        {
            string thongbao = "";
            thongbao = Kiemtradangky();
            if(thongbao == "")
            {
                //insert vao csdl
                try
                {                   
                    dservice.Connect();

                    //them nguoi dung moi
                    SqlParameter[] parameters = new SqlParameter[2];
                    parameters = new SqlParameter[6];
                    parameters[0] = new SqlParameter("@username", txtUser.Text);
                    parameters[1] = new SqlParameter("@pass", txtPass.Text);
                    parameters[2] = new SqlParameter("@hoten",txtHoTen.Text );
                    parameters[3] = new SqlParameter("@cmnd", txtCMND.Text);
                    parameters[4] = new SqlParameter("@diachi",txtDiaChi.Text );
                    parameters[5] = new SqlParameter("@dienthoai", txtDienThoai.Text);
                    dservice.ExcuteProcedure("sp_DangKy", parameters, null);

                    dservice.Disconnect();
                    MessageBox.Show("!!! Đăng ký thành công !!!");
                    Public_variable.Username = txtUser.Text;
                    Public_variable.Hoten = txtHoTen.Text; 
                    frm.load();
                    //clear
                    txtDienThoai.Text = "";
                    txtHoTen.Text = "";
                    txtPass.Text = "";
                    txtRePass.Text = "";
                    txtUser.Text = "";
                    txtDiaChi.Text = "";
                    txtCMND.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show(thongbao);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            //FormMain1 frm = new FormMain1();
            
        }
        public String Kiemtradangky() // viet ham kiem tra ko có thuoc tinh rong,kiem tra ko trung user name ok
        {
            string s = "";
            //DataService dservice = new DataService();

            //dservice.Connect();
            //SqlParameter[] parameters = new SqlParameter[1];
            //parameters[0] = new SqlParameter("@username", txtUser.Text);
            //int kq = dservice.ExcuteProcedureWithReturn("gdf", parameters);

            if (true)
            { 
                // kiem tra neu co thuoc tinh rong thi gan thong bao
               // s+= " không được để trống thuộc tính";

                // kiem tra pass trung nhau hay ko,gan thong bao
                if (txtCMND.Text == "" || txtDiaChi.Text == "" || txtDienThoai.Text == "" || txtHoTen.Text == "" || txtPass.Text == "" || txtRePass.Text == "" || txtUser.Text == "")
                {
                    s += "Thông tin đăng ký chưa đầy đủ - Vui lòng kiểm tra!!!";
                }
                else
                {
                    dservice.Connect();
                                       
                    SqlParameter[] parameters = new SqlParameter[1];
                    parameters[0] = new SqlParameter("@username", txtUser.Text);
                    dt= dservice.ExcuteProcedureGetTable ("sp_KtraTrungUser", parameters,null);
                    dservice.Disconnect();
                    if (dt.Rows.Count> 0)
                    {
                        s += "Trùng UserName - Vui lòng chọn UserName khác!!!";
                    }
                    else
                    {
                        if (txtPass.Text != txtRePass.Text)
                        {
                            s += "Repassword không trùng với pass!";
                        }
                        else
                        {
                            s += "";
                        }
                    }
                }

            }
            // goi store kiem tra neu user nam trung thi cung gan 
          //  s += " trung username hay chon username khác";
            return s;
        }
    }
}
